package cn.wgx.module.mind.websocket.entity;


import cn.wgx.common.util.UUIDUtils;
import lombok.Data;

import java.io.Serializable;

@Data
public class WSPrincipal implements java.security.Principal, Serializable {

    //必备固定名称name,唯一
    private String name = "U"+UUIDUtils.getGUID32();

    //用户基本信息
    private Integer user_id;
    protected String username;
    protected String nickname;
    private String photo;


    private int type;
    private int state;
    private long limit;
    private String roomId;

    //是否管理员
    private boolean isAdmin = false;

    //是否被禁言
    private boolean isForbidden = false;



    @Override
    public String getName() {
        return this.name;
    }

    public WSPrincipal(){}

    public WSPrincipal(String name){
        this.name = name;
        try {
            this.user_id = Integer.parseInt(name);
        }catch (Exception e){
            System.out.println("websocket用户名错误,需要数字name");
        }
    }


    public void leftAllRoom(){
        //OnlineUserTool.delUserAllRoom(this);
    }

    @Override
    public boolean equals(Object o){
        if(o instanceof WSPrincipal){
            return this.name.equals(((WSPrincipal) o).getName());
        }
        return  false;
    }

    @Override
    public int hashCode(){
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        char[] charArr = sb.toString().toCharArray();
        int hash = 0;
        for(char c : charArr) {
            hash = hash * 131 + c;
        }
        return hash;
    }
}
